//test first line
package au.chao.main;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import au.chao.utils.GraphUtility;
import au.chao.utils.Validator;

public class TenantEnterForm extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5331528407843076207L;
	
	public static final int FLAG_ADD = 0;
	public static final int FLAG_EDIT = 1;
	
	private int addOrEdit = -1;
	
	private JTable tenantTable;

	final JTextField txtFullName = new JTextField();

	final JTextField txtStartDate = new JTextField();

	final JTextField txtEndDate = new JTextField();

	final JTextField txtRental = new JTextField();

	public TenantEnterForm(int flag) {
		super();
		addOrEdit = flag;
		initGUI();
	}

	public TenantEnterForm(JTable tbl, int flag) {
		super();
		tenantTable = tbl;
		addOrEdit = flag;
		initGUI();

	}

	void initGUI() {

		Container mainCnt = new Container();
		GridLayout lay = new GridLayout(2, 4);
		lay.setHgap(10);
		lay.setVgap(10);
		mainCnt.setLayout(lay);

		JLabel lbFullName = new JLabel("Full Name");
		JLabel lbStartDate = new JLabel("Start Date");
		JLabel lbEndDate = new JLabel("End Date");
		JLabel lbRental = new JLabel("Rental");

		mainCnt.add(lbFullName);
		mainCnt.add(txtFullName);
		mainCnt.add(lbRental);
		mainCnt.add(txtRental);
		mainCnt.add(lbStartDate);
		mainCnt.add(txtStartDate);
		mainCnt.add(lbEndDate);
		mainCnt.add(txtEndDate);

		Container btnCnt = new Container();
		FlowLayout flay = new FlowLayout();
		flay.setHgap(10);
		btnCnt.setLayout(flay);

		JButton btnAdd = new JButton("Save");
		btnAdd.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				if(addOrEdit == FLAG_ADD){
				Object[] tenantDetails = new Object[] { txtFullName.getText(),
						txtStartDate.getText(), txtEndDate.getText(),
						txtRental.getText() };
				if (validInput(tenantDetails)) {
					((TenantTableModel) tenantTable.getModel())
							.addRow(tenantDetails);
					dispose();
				} else {
					JOptionPane.showMessageDialog(null, "Check Input Formate",
							"alert", JOptionPane.ERROR_MESSAGE);
				}

				((TenantTableModel) tenantTable.getModel())
						.addRow(tenantDetails);
				}
				else{
					int selectedRowNum = tenantTable.getSelectedRow();
					tenantTable.setValueAt(txtFullName.getText(),selectedRowNum, 0);
					tenantTable.setValueAt(txtStartDate.getText(),selectedRowNum, 1);
					tenantTable.setValueAt(txtEndDate.getText(),selectedRowNum, 2);
					tenantTable.setValueAt(txtRental.getText(),selectedRowNum, 3);
				}
				dispose();
			}

		});
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				dispose();
			}
		});
		btnCnt.add(btnAdd);
		btnCnt.add(btnCancel);

		setLayout(new GridBagLayout());
		GridBagConstraints topCS = new GridBagConstraints();
		topCS.insets = new Insets(10, 10, 10, 10);
		topCS.gridx = 0;
		topCS.gridy = 0;
		topCS.weighty = 1;
		topCS.anchor = GridBagConstraints.PAGE_START;
		topCS.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(mainCnt, topCS);

		GridBagConstraints bottomCS = new GridBagConstraints();
		bottomCS.gridx = 0;
		bottomCS.gridy = 1;
		bottomCS.weightx = 1;
		bottomCS.weighty = 0.1;
		topCS.anchor = GridBagConstraints.PAGE_END;
		bottomCS.fill = GridBagConstraints.HORIZONTAL;
		bottomCS.insets = new Insets(10, 10, 10, 10);
		getContentPane().add(btnCnt, bottomCS);

		setModal(true);
		pack();
		GraphUtility.centerWindow(this);

	}

	private boolean validInput(Object[] input) {
		if (((String) input[0]).length() < 1)
			return false;
		if (!Validator.validDate((String) input[1])) {
			return false;
		}
		if (!Validator.validDate((String) input[2])) {
			return false;
		}
		if (!Validator.validNumber((String) input[3])) {
			return false;
		}
		return true;
	}

	public void populateData() {
		if (tenantTable != null) {
			int selectedRowNum = tenantTable.getSelectedRow();
			String fullName = (String) ((TenantTableModel) tenantTable
					.getModel()).getValueAt(selectedRowNum, 0);
			String startDate = (String) ((TenantTableModel) tenantTable
					.getModel()).getValueAt(selectedRowNum, 1);
			String endDate = (String) ((TenantTableModel) tenantTable
					.getModel()).getValueAt(selectedRowNum, 2);
			String rental = (String) ((TenantTableModel) tenantTable.getModel())
					.getValueAt(selectedRowNum, 3);
			txtFullName.setText(fullName);
			txtStartDate.setText(startDate);
			txtEndDate.setText(endDate);
			txtRental.setText(rental);
		}
	}
}
